package img;

import java.awt.image.BufferedImage;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 10:37:20 2018/11/16
 * @version:2.0
 */
public class PixelsUtil {

    //获取颜色代码[横，竖]遍历是一竖一竖的遍历过去
    public static int[][] getImageRGB(BufferedImage bfImage) {
        int width = bfImage.getWidth();
        int height = bfImage.getHeight();
        int[][] result = new int[width][height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                //使用getRGB(w, h)获取该点的颜色值是ARGB，而在
                // 实际应用中使用的是RGB，所以
                // 需要将ARGB转化成RGB，即bufImg.getRGB(w, h) & 0xFFFFFF。
                result[i][j] = bfImage.getRGB(i, j) & 0xFFFFFF;
            }
        }
        return result;
    }

    //获取固定位置
    public static int[][] getPixelsInFiled(int[][] pixels, int top, int left, int bottom, int right) {
        int startWidth = left;
        int endWidth = pixels.length - right;
        int startHeight = top;
        int endHeight = pixels[0].length - bottom;
        int[][] pixelsNew = new int[endWidth - startWidth][endHeight - startHeight];
        for (int i = startWidth; i < endWidth; i++) {
            for (int j = startHeight; j < endHeight; j++) {
                pixelsNew[i - startWidth][j - startHeight] = pixels[i][j];
            }
        }
        return pixelsNew;
    }
    //去除指定颜色
    public static void excludeColor(int[] excludes, int[][] pixels, int toColor) {
        for (int color : excludes) {
            for (int i = 0; i < pixels.length; i++) {
                for (int j = 0; j < pixels[i].length; j++) {
                    // System.out.print(pixels[i] [j]+" ");
                    boolean same = pixels[i][j] == color;
                    if (!same) {
                        pixels[i][j] =toColor;// new Color(255, 255, 255).getRGB();
                    }
                }
                //System.out.println();
            }
        }
    }
    //去范围颜色
    public static void excludeFiled(int base, int[][] pixels, int toColor) {
            for (int i = 0; i < pixels.length; i++) {
                for (int j = 0; j < pixels[i].length; j++) {
                    // System.out.print(pixels[i] [j]+" ");
                    boolean same = pixels[i][j] <base;
                    if (!same) {
                        pixels[i][j] =toColor;// new Color(255, 255, 255).getRGB();
                    }
                }
                //System.out.println();
            }
    }
}
